<template>
  <a-layout id="components-layout-demo-custom-trigger">
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" :theme="navTheme">
      <div class="logo"></div>
      <a-menu v-model:selectedKeys="selectedKeys" style="width: 100%" mode="inline" :theme="navTheme"
        :open-keys="openKeys" @openChange="onOpenChange">
        <template v-for="item in routesTree" :key="item.path">
          <template v-if="!item.children">
            <RouterLink :to="item.path">
              <a-menu-item :key="item.path">
                <template #icon>
                  <component :is="item.meta.icon"></component>
                </template>
                {{ item.meta.title }}
              </a-menu-item>
            </RouterLink>
          </template>
          <template v-else>
            <sub-menu :key="item.path" :menu-info="item" />
          </template>
        </template>
      </a-menu>

    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <a-row>
          <a-col :span="22">
            <menu-unfold-outlined v-if="collapsed" class="trigger" @click="() => (collapsed = !collapsed)" />
            <menu-fold-outlined v-else @click="() => (collapsed = !collapsed)" class="trigger" />
          </a-col>
          <a-col :span="2">
            <a-dropdown>
              <template #overlay>
                <a-menu @click="handleMenuClick">
                  <a-menu-item key="1">
                    <UserOutlined />
                    退出登录
                  </a-menu-item>
                </a-menu>
              </template>
              <a-button>
                设置
                <DownOutlined />
              </a-button>
            </a-dropdown>
          </a-col>
        </a-row>
      </a-layout-header>
      <a-layout-content :style="{ margin: '24px 16px', padding: '24px', background: '#fff', minHeight: '280px' }">
        <RouterView></RouterView>
      </a-layout-content>
    </a-layout>
  </a-layout>

<!--  <Authorized :permissions="['admin', 'users']">-->
<!--  <SettingDrawer></SettingDrawer>-->
<!--  </Authorized>-->
</template>

<script setup>
import {
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  UserOutlined,
  DownOutlined
} from '@ant-design/icons-vue'

import store from 'store'
import SettingDrawer from '@/components/SettingDrawer.vue'

import { ref, watch, onMounted, watchEffect } from 'vue'
import { useRoute, useRouter } from 'vue-router'

import fetchRoutesTree from '@/api/routesTree.js'
import SubMenu from '@/components/SubMenu.vue'
import axios from "axios";

const router = useRouter()
const route = useRoute()

const navTheme = ref('dark')

const routesTree = ref([])
const collapsed = ref(false)
const selectedKeys = ref([])
const openKeys = ref([])
const rootSubmenuKeys = ref([])

const handleMenuClick = () => {
  store.remove('access_token')
  window.location.reload()
}

const setOpenKeys = (r) => {
  openKeys.value = [r.matched[r.matched.length - 2].path]
  selectedKeys.value = [r.fullPath]
}

watch(route, (r) => {
  setOpenKeys(r)
})

watchEffect(() => {
  navTheme.value = route.query.navTheme || 'dark'
})

onMounted(async () => {
  const  result = await fetchRoutesTree()

  routesTree.value = result;
  rootSubmenuKeys.value = routesTree.value.map(r => r.path)
  setOpenKeys(route)
})

const onOpenChange = allOpenKeys => {
  // laterstOpenKey 当前打开的 menu
  const latestOpenKey = allOpenKeys.find(key => {
    return openKeys.value.indexOf(key) === -1
  })

  if (rootSubmenuKeys.value.indexOf(latestOpenKey) === -1) {
    openKeys.value = allOpenKeys;
  } else {
    openKeys.value = latestOpenKey ? [latestOpenKey] : [];
  }

  // 设置当前选中的菜单
  router.push(latestOpenKey || [])
}
</script>

<style>
#components-layout-demo-custom-trigger {
  height: 100%;
}

#components-layout-demo-custom-trigger .trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

#components-layout-demo-custom-trigger .trigger:hover {
  color: #1890ff;
}

#components-layout-demo-custom-trigger .logo {
  height: 32px;
  background: rgba(255, 255, 255, 0.3);
  margin: 16px;
}

.site-layout .site-layout-background {
  background: #fff;
}
</style>
